package com.leetcode.LC_MED;

import java.util.ArrayList;
import java.util.List;

public class LC22 {
    public List<String> generateParenthesis(int n) {
        //思路：左括号数量必须大于等于右括号数量，当左括号数量等于右括号数量等于n则返回结果集
        ArrayList<String> list = new ArrayList<>();
        if (n == 1) {
            list.add("()");
            return list;
        }
        int countR = 0, countL = 0;
        toCreate(n, list, countL, countR, "");
        return list;
    }

    public void toCreate(int n, List<String> list, int left, int right, String str) {

        if (right > left) {
            return;
        }
        if (right == left && right == n) {
            list.add(str);
        }
        if (left < n) {
            toCreate(n, list, left + 1, right, str + "(");
        }
        if (right < left) {
            toCreate(n, list, left, right + 1, str + ")");
        }
    }

    public static void main(String[] args) {
        for (String s : new LC22().generateParenthesis(3)) {
            System.out.println(s);
        }
    }
}
